<?php
namespace StarCloud\Library\Sdk;

use Framework\DiServices\HttpClient;
use Phalcon\Di;
use StarCloud\Library\HttpCurl;

class Oceanengine
{
    use HttpCurl;

    /* @var $httpClient HttpClient */
    private $httpClient = null;

    private $app_id = 1803454486264320;
    private $secret = '49505b860c432790fb4287846feafc5e059bdc9d';
    private $access_token = "";
    private $header = [
        'Content-Type' => 'application/json',
    ];

    public function __construct()
    {
        $this->httpClient = Di::getDefault()->get('httpClient');
        $this->cacheClass = Di::getDefault()->get('redis');
    }

    public function auth()
    {

        $accessToken = $this->getCache($this->getTokenKey());
        if (!empty($accessToken)) {
            $this->access_token = $accessToken;
            return true;
        }
        $url = 'https://ad.oceanengine.com/open_api/oauth2/access_token/';
        $body = [
            'app_id' => $this->app_id,
            'secret' => $this->secret,
            "grant_type" => "auth_code",
            'auth_code' => $_GET['auth_code']
        ];

        $response = $this->httpClient->sendPostRequest($url, $body, $this->header);

        $json = json_decode($response->getBody()->getContents(), true);
        if (isset($json['data']['access_token'])) {
            $this->setCache($this->getTokenKey(), $json['data']['access_token'], $json['data']['expires_in'] - 200);
            $this->setCache($this->getRefreshTokenKey(), $json['data']['refresh_token'], $json['data']['refresh_token_expires_in'] - 200);
            return $json;
        }

        return false;
    }

    public function getTokenKey()
    {
        return "oceanengine_access_token_app_id_" . $this->app_id;
    }

    public function getRefreshTokenKey()
    {
        return "oceanengine_refresh_access_token_app_id_" . $this->app_id;
    }

    public function refreshToken()
    {
        $refreshToken = $this->getCache($this->getRefreshTokenKey());
        $url = 'https://ad.oceanengine.com/open_api/oauth2/refresh_token/';
        $body = [
            'app_id' => $this->app_id,
            'secret' => $this->secret,
            'refresh_token' => $refreshToken
        ];

        $response = $this->httpClient->sendPostRequest($url, $body, $this->header);

        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['data']['access_token'])) {
            $this->setCache($this->getTokenKey(), $json['data']['access_token'], $json['data']['expires_in'] - 200);
            $this->setCache($this->getRefreshTokenKey(), $json['data']['refresh_token'], $json['data']['refresh_token_expires_in'] - 200);
            return true;
        }

        return false;
    }

    // 获取未参与的任务列表
    public function getUnparticipatedTasks($starId, $page = 1, $size = 10)
    {
        $url = 'https://api.oceanengine.com/open_api/2/star/mcn/get_unparticipated_task/?';
        $accessToken = $this->getCache($this->getTokenKey());
        $header = $this->header;
        $header['Access-Token'] = $accessToken;
        $body = [
            'star_id' => $starId,
            'page' => $page,
            'page_size' => $size,
        ];
        $response = $this->httpClient->sendGetRequest($url . http_build_query($body), $body, $header);

        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['data'])) {
            return $json['data'];
        }
        var_dump($json);
        return [];
    }

    // 获取已经参与的任务列表
    public function getparticipatedTasks($starId, $page = 1, $size = 10)
    {
        $url = 'https://api.oceanengine.com/open_api/2/star/mcn/get_contracted_challenge_list/?';
        $accessToken = $this->getCache($this->getTokenKey());
        $header = $this->header;
        $header['Access-Token'] = $accessToken;
        $body = [
            'star_id' => $starId,
            'page' => $page,
            'page_size' => $size,
        ];
        $response = $this->httpClient->sendGetRequest($url . http_build_query($body), $body, $header);

        $json = json_decode($response->getBody()->getContents(), true);
        if (isset($json['data'])) {
            return $json['data'];
        }
        return [];
    }

    public function joinTask($starId, $taskId, $ratio = 1000)
    {
        $url = 'https://api.oceanengine.com/open_api/2/star/mcn/contract_challenge/';
        $accessToken = $this->getCache($this->getTokenKey());
        $header = $this->header;
        $header['Access-Token'] = $accessToken;
        $body = [
            'star_id' => intval($starId),
            'demand_id' => $taskId,
            'mcn_profit_rate' => $ratio,
        ];
        $response = $this->httpClient->sendPostRequest($url, $body, $header);
        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['data']['result']) && $json['data']['result']) {
            return true;
        }

        var_dump($json);

        return false;
    }

    public function get_contracted_challenge_url($starId, $taskId, $uid)
    {
        $url = 'https://api.oceanengine.com/open_api/2/star/mcn/get_contracted_challenge_url/?';
        $accessToken = $this->getCache($this->getTokenKey());
        $header = $this->header;
        $header['Access-Token'] = $accessToken;
        $body = [
            'star_id' => $starId,
            'demand_id' => $taskId,
            'channel_id' => $uid,
        ];
        $response = $this->httpClient->sendGetRequest($url . http_build_query($body), $body, $header);

        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['data']['task_url'])) {
            return $json['data']['task_url'];
        }

        return false;
    }

    public function getOrderList($starId, $taskId, $page = 1)
    {
        $url = 'https://api.oceanengine.com/open_api/2/star/mcn/get_contract_challenge_author_item_list_v2/?';
        $accessToken = $this->getCache($this->getTokenKey());
        $header = $this->header;
        $header['Access-Token'] = $accessToken;
        $body = [
            'star_id' => $starId,
            'demand_id' => $taskId,
            'page' => $page,
            'page_size' => 30,
        ];

        $response = $this->httpClient->sendGetRequest($url . http_build_query($body), $body, $header);

        $json = json_decode($response->getBody()->getContents(), true);
        if (isset($json['data']['data'])) {
            return $json['data']['data'];
        }
        return false;
    }

    public function getVertiser()
    {
        $url = 'https://ad.oceanengine.com/open_api/oauth2/advertiser/get/?';
        $accessToken = $this->getCache($this->getTokenKey());
        $body = [
            'access_token' => $accessToken
        ];
        $response = $this->httpClient->sendGetRequest($url . http_build_query($body), $body, $this->header);

        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['data']['list'])) {
            return $json['data']['list'];
        }

        return [];
    }
}